package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import cn.edu.tju.core.model.User;
import jakarta.persistence.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "orders")
public class Order extends BaseEntity {

    @ManyToOne
    @JoinColumn(name = "customer_id", nullable = false)
    private User customer;

    @ManyToOne
    @JoinColumn(name = "business_id", nullable = false)
    private Business business;

    private LocalDateTime orderDate;

    @Column(precision = 10, scale = 2, nullable = false)
    private BigDecimal orderTotal;

    @ManyToOne
    @JoinColumn(name = "address_id", nullable = false)
    private DeliveryAddress deliveryAddress;
    
    // 添加地址快照字段
    @Column(length = 50)
    private String contactName; // 联系人姓名
    
    private Integer contactSex; // 联系人性别
    
    @Column(length = 20)
    private String contactTel; // 联系电话
    
    @Column(length = 255)
    private String address; // 详细地址
    
    private Integer orderState;

    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnoreProperties("order")
    private List<OrderDetailet> orderDetailets = new ArrayList<>();

    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    public Business getBusiness() {
        return business;
    }

    public void setBusiness(Business business) {
        this.business = business;
    }

    public LocalDateTime getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(LocalDateTime orderDate) {
        this.orderDate = orderDate;
    }

    public BigDecimal getOrderTotal() {
        return orderTotal;
    }

    public void setOrderTotal(BigDecimal orderTotal) {
        this.orderTotal = orderTotal;
    }

    public DeliveryAddress getDeliveryAddress() {
        return deliveryAddress;
    }

    public void setDeliveryAddress(DeliveryAddress deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
        // 自动设置地址快照字段
        if (deliveryAddress != null) {
            this.contactName = deliveryAddress.getContactName();
            this.contactSex = deliveryAddress.getContactSex();
            this.contactTel = deliveryAddress.getContactTel();
            this.address = deliveryAddress.getAddress();
        }
    }

    public Integer getOrderState() {
        return orderState;
    }

    public void setOrderState(Integer orderState) {
        this.orderState = orderState;
    }

    public List<OrderDetailet> getOrderDetailets() {
        return orderDetailets;
    }

    public void setOrderDetailets(List<OrderDetailet> orderDetailets) {
        this.orderDetailets = orderDetailets;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public Integer getContactSex() {
        return contactSex;
    }

    public void setContactSex(Integer contactSex) {
        this.contactSex = contactSex;
    }

    public String getContactTel() {
        return contactTel;
    }

    public void setContactTel(String contactTel) {
        this.contactTel = contactTel;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
